package cn.wildfire.chat.app.extendsion.utils;

import android.os.AsyncTask;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Author by HEKE
 *
 * @time 2017/12/1 9:21
 * TODO: 文件操作封装对象
 */
public class FileUtils {

    /**
     * 创建目录
     *
     * @param absolutelyDir
     */
    public static boolean createDirectory(String absolutelyDir) {
        if (TextUtils.isTextEmpty(absolutelyDir))
            return false;
        try {
            File dir = new File(absolutelyDir);
            if (!dir.exists()) {
                dir.setExecutable(true);
                dir.setReadable(true);
                dir.setWritable(true);
                dir.mkdirs();
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 创建文件
     *
     * @param absolutelyFile
     */
    public static boolean createFile(String absolutelyFile) {
        if (TextUtils.isTextEmpty(absolutelyFile))
            return false;
        try {
            File file = new File(absolutelyFile);
            if (!file.exists()) {
                file.setExecutable(true);
                file.setReadable(true);
                file.setWritable(true);
                file.createNewFile();
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 删除文件
     *
     * @param filePath
     */
    public static boolean deleteFile(String filePath) {
        if (TextUtils.isTextEmpty(filePath))
            return false;
        try {
            File file = new File(filePath);
            if (file.exists()) {
                file.delete();
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 删除目录下文件
     *
     * @param dir
     */
    public static boolean deleteAllFiles(String dir) {
        if (TextUtils.isTextEmpty(dir))
            return false;
        try {
            File dirFile = new File(dir);
            if (dirFile.exists()) {
                File[] files = dirFile.listFiles();
                if (files != null) {
                    for (int i = 0; i < files.length; i++) {
                        File file = files[i];
                        if (file.isFile()) {
                            file.delete();
                        }
                    }
                }
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 保存本地文件复制
     *
     * @param inputStream
     * @param filePath
     * @param onFileSaveListener
     */
    public static void savePicAsync(InputStream inputStream, String filePath, OnFileSaveListener onFileSaveListener) {
        new AsyncTask<String, Integer, String>() {
            @Override
            protected String doInBackground(String... strings) {
                try {
                    OutputStream os = null;
                    try {
                        // 2、保存到临时文件
                        // 数据缓冲
                        byte[] bs = new byte[inputStream.available()];
                        inputStream.read(bs);
                        // 输出的文件流保存到本地文件
                        os = new FileOutputStream(filePath);
                        // 开始读取
                        os.write(bs);
                    } catch (Exception e) {
                        e.printStackTrace();
                        return "0";
                    } finally {
                        // 完毕，关闭所有链接
                        try {
                            os.close();
                            inputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return "1";
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                if (onFileSaveListener != null) {
                    onFileSaveListener.onSaved(s.equals("1"));
                }
            }

        }.execute();
    }

    public interface OnFileSaveListener {
        void onSaved(boolean success);
    }

}
